from datetime import datetime
from fastapi import APIRouter, Depends, Form, Request
from pydantic_validation_decorator import ValidateFields
from sqlalchemy.ext.asyncio import AsyncSession
from config.enums import BusinessType
from config.get_db import get_db
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.kq_user_vo import Kq_userPageQueryModel, Kq_userModel, DeleteKq_userModel
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.service.kq_user_service import Kq_userService
from module_admin.service.login_service import LoginService

from utils.common_util import bytes2file_response
from utils.log_util import logger
from utils.page_util import PageResponseModel
from utils.response_util import ResponseUtil


kq_userController = APIRouter(prefix='/kaoqin/kq_user', dependencies=[Depends(LoginService.get_current_user)])


@kq_userController.get(
    '/list', response_model=PageResponseModel, dependencies=[Depends(CheckUserInterfaceAuth('kaoqin:kq_user:list'))]
)
async def get_kaoqin_kq_user_list(
    request: Request,
kq_user_page_query: Kq_userPageQueryModel = Depends(Kq_userPageQueryModel.as_query),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取分页数据
    kq_user_page_query_result = await Kq_userService.get_kq_user_list_services(query_db, kq_user_page_query, is_page=True)
    logger.info('获取成功')

    return ResponseUtil.success(model_content=kq_user_page_query_result)


@kq_userController.post('', dependencies=[Depends(CheckUserInterfaceAuth('kaoqin:kq_user:add'))])
@ValidateFields(validate_model='add_kq_user')
@Log(title='考勤用户花名册', business_type=BusinessType.INSERT)
async def add_kaoqin_kq_user(
    request: Request,
    add_kq_user: Kq_userModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    add_kq_user.create_by = current_user.user.user_name
    add_kq_user.create_time = datetime.now()
    add_kq_user.update_by = current_user.user.user_name
    add_kq_user.update_time = datetime.now()
    add_kq_user_result = await Kq_userService.add_kq_user_services(query_db, add_kq_user)
    logger.info(add_kq_user_result.message)

    return ResponseUtil.success(msg=add_kq_user_result.message)


@kq_userController.put('', dependencies=[Depends(CheckUserInterfaceAuth('kaoqin:kq_user:edit'))])
@ValidateFields(validate_model='edit_kq_user')
@Log(title='考勤用户花名册', business_type=BusinessType.UPDATE)
async def edit_kaoqin_kq_user(
    request: Request,
    edit_kq_user: Kq_userModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    edit_kq_user.update_by = current_user.user.user_name
    edit_kq_user.update_time = datetime.now()
    edit_kq_user_result = await Kq_userService.edit_kq_user_services(query_db, edit_kq_user)
    logger.info(edit_kq_user_result.message)

    return ResponseUtil.success(msg=edit_kq_user_result.message)


@kq_userController.delete('/{ids}', dependencies=[Depends(CheckUserInterfaceAuth('kaoqin:kq_user:remove'))])
@Log(title='考勤用户花名册', business_type=BusinessType.DELETE)
async def delete_kaoqin_kq_user(request: Request, ids: str, query_db: AsyncSession = Depends(get_db)):
    delete_kq_user = DeleteKq_userModel(ids=ids)
    delete_kq_user_result = await Kq_userService.delete_kq_user_services(query_db, delete_kq_user)
    logger.info(delete_kq_user_result.message)

    return ResponseUtil.success(msg=delete_kq_user_result.message)


@kq_userController.get(
    '/{id}', response_model=Kq_userModel, dependencies=[Depends(CheckUserInterfaceAuth('kaoqin:kq_user:query'))]
)
async def query_detail_kaoqin_kq_user(request: Request, id: int, query_db: AsyncSession = Depends(get_db)):
    kq_user_detail_result = await Kq_userService.kq_user_detail_services(query_db, id)
    logger.info(f'获取id为{id}的信息成功')

    return ResponseUtil.success(data=kq_user_detail_result)


@kq_userController.post('/export', dependencies=[Depends(CheckUserInterfaceAuth('kaoqin:kq_user:export'))])
@Log(title='考勤用户花名册', business_type=BusinessType.EXPORT)
async def export_kaoqin_kq_user_list(
    request: Request,
    kq_user_page_query: Kq_userPageQueryModel = Form(),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取全量数据
    kq_user_query_result = await Kq_userService.get_kq_user_list_services(query_db, kq_user_page_query, is_page=False)
    kq_user_export_result = await Kq_userService.export_kq_user_list_services(kq_user_query_result)
    logger.info('导出成功')

    return ResponseUtil.streaming(data=bytes2file_response(kq_user_export_result))
